package api_policy

import (
	"encoding/json"
	"fmt"
	"io"
	"restapi/data"
	"restapi/module" 
	"strconv"
	"github.com/gin-gonic/gin"
)

// 通用响应封装
func respond400(g *gin.Context, msg string) {
	g.JSON(400, gin.H{"code": 400, "message": msg})
}

// 读取并反序列化 JSON（不使用 ShouldBindJSON，便于后续自定义缺失字段处理）
func decodeJSON(g *gin.Context, dst interface{}) bool {
	body, err := io.ReadAll(g.Request.Body)
	//获取请求正文
	if err != nil {
		respond400(g, "读取请求体失败")
		return false
	}
	//检查请求正文是否为空
	if len(body) == 0 {
		respond400(g, "请求体不能为空")
		return false
	}
	//反序列化
	if err := json.Unmarshal(body, dst); err != nil {
		respond400(g, "JSON格式错误")
		return false
	}
	return true
}

// 手动校验创建策略存在性
func validateCreatePolicy(req *module.CreatePolicyRequest) (ok bool, msg string) {
	if req.Name == "" {
		return false, "策略名称为空"
	}
	if req.Protocol == "" {
		return false, "协议为空"
	}
	if req.ListenInterface == "" {
		return false, "监听网口为空"
	}
	if req.ListenPort == 0 {
		return false, "监听端口为空或为0"
	}
	if req.ProxyAddress == "" {
		return false, "代理地址不能为空"
	}
	if req.ProxyPort == 0 {
		return false, "代理端口不能为空或为0"
	}
	if req.ForwardInterface == "" {
		return false, "转发网口不能为空"
	}
	if req.ForwardAddress == "" {
		return false, "转发地址不能为空"
	}
	if len(req.AccessControl) == 0 {
		return false, "访问控制不能为空"
	}
	if req.VsysId == 0 {
		return false, "虚拟系统ID不能为空或"
	}
	return true, ""
}

// 手动校验创建策略冲突性
func validatePolicyConflict(req *module.CreatePolicyRequest) (ok bool, msg string) {
	//名字唯一
	if result, err := data.IsNameConflict(req.Name); err != nil || result {
		return false, fmt.Sprintf("策略名称(%s)已存在", req.Name)
	} else if result, err = data.IsPolicyConflict(req.Protocol, req.ListenInterface, req.ListenPort, req.VsysId); err != nil || result {
		return false, "策略协议、网口、端口同时存在"
	}
	return true, ""
}

// 手动校验更新策略（当前与创建一致，后续若支持部分更新可调整）
func validateUpdatePolicy(req *module.UpdatePolicyRequest) (ok bool, msg string) {
	if req.Id == 0 {
		return false, "虚拟系统ID不能为空或为0"
	}
	if req.Name == "" {
		return false, "策略名称不能为空"
	}
	if req.Protocol == "" {
		return false, "协议不能为空"
	}
	if req.ListenInterface == "" {
		return false, "监听网口不能为空"
	}
	if req.ListenPort == 0 {
		return false, "监听端口不能为空或为0"
	}
	if req.ProxyAddress == "" {
		return false, "代理地址不能为空"
	}
	if req.ProxyPort == 0 {
		return false, "代理端口不能为空或为0"
	}
	if req.ForwardInterface == "" {
		return false, "转发网口不能为空"
	}
	if req.ForwardAddress == "" {
		return false, "转发地址不能为空"
	}
	if len(req.AccessControl) == 0 {
		return false, "访问控制不能为空"
	}
	if req.VsysId == 0 {
		return false, "虚拟系统ID不能为空或为0"
	}
	return true, ""
}

// 列表查询校验（与 vsys 风格统一）
func validateListPolicy(pageSize, pageNum, vsysId int) (bool, string) {
	if pageSize <= 0 {
		return false, "页大小必须大于0"
	}
	if pageNum <= 0 {
		return false, "页码必须大于0"
	}
	if vsysId <= 0 {
		return false, "虚拟系统ID必须大于0"
	}
	return true, ""
}

// 统一解析策略列表查询参数（与 vsys List 一致的手动解析风格）
func parseListPolicyQuery(g *gin.Context, req *module.ListPolicyRequest) {
	if pageSizeStr := g.Query("pageSize"); pageSizeStr != "" {
		req.PageSize, _ = strconv.Atoi(pageSizeStr)
	}
	if pageNumStr := g.Query("pageNum"); pageNumStr != "" {
		req.PageNum, _ = strconv.Atoi(pageNumStr)
	}
	if vsysIdStr := g.Query("vsysId"); vsysIdStr != "" {
		req.VsysId, _ = strconv.Atoi(vsysIdStr)
	}
	req.Name = g.Query("name")
}
